﻿/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;

namespace LucasCode.PublicTypes
{
    public class UploadFile
    {
        public class ProgressChangedEventArgs : EventArgs
        {
            UploadFile m_File;
            public UploadFile File
            {
                get { return m_File; }
                set { m_File = value; }
            }

            public ProgressChangedEventArgs(UploadFile file)
            {
                m_File = file;
            }
        };

        readonly MatchFile m_File;
        public UploadFile(MatchFile file)
        {
            m_File = file;
        }

        public bool Aborted
        {
            get;
            set;
        }
        public void Cancel()
        {
            Aborted = true;
        }

        /*public void Reset()
        {
            Aborted = false;
            m_Progress = 0;
            m_Status = string.Empty;
        }*/

        public MatchFile MatchFile
        {
            get
            {
                return m_File;
            }
        }

        int m_Progress;
        public int Progress
        {
            get { return m_Progress; }
            set
            {
                if (value == m_Progress)
                    return;
                m_Progress = value;
                OnProgressChanged();
            }
        }

        private string m_Status;
        public string Status
        {
            get{return m_Status;}
            set
            {
                if (value == m_Status)
                    return;
                m_Status = value;
                OnProgressChanged();
            }
        }
        private int m_Speed;
        public int Speed
        {
            get
            {
                return m_Speed;
            }
        }
        public void TransferredBytesDelta(int count)
        {
            m_SpeedMonitor.AddDelta(count);
            m_SpeedMonitor.Tick();
            if (m_Speed != m_SpeedMonitor.Rate)
                m_Speed = m_SpeedMonitor.Rate;
            m_Progress = m_SpeedMonitor.Progress;
            OnProgressChanged();
        }
        public void ResetTransferStats()
        {
            m_SpeedMonitor.Reset(0);
        }
        public void ResetTransferStats(long filesize)
        {
            m_SpeedMonitor.Reset(filesize);
        }
        protected void OnProgressChanged()
        {
            EventHandler<ProgressChangedEventArgs> h = ProgressChanged;
            if (h != null)
                h(this, new ProgressChangedEventArgs(this));
        }
        public event EventHandler<ProgressChangedEventArgs> ProgressChanged;
        //public event EventHandler<FileStatusChangedEventArgs> FileStatusChanged;
        private readonly SpeedMonitor m_SpeedMonitor = new SpeedMonitor(5);
    }
}
